﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.GIS;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.CallRoute;
using System.Net;
using Jumpton.SFA.Server.BusinessRule.MasterData;

public partial class GIS_VisitRoute : PageBase, ICallbackEventHandler
{
    GISManager gisManager = new GISManager();
    StateManager stateManager = new StateManager();
    OrgainzationManager orgManager = new OrgainzationManager();
    public string _content;
    int no = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            BindUser();
            if (txtHidOrg.Value == "")
            {
                InitForm();
                DataBind();
                LoadMaps();

            }
        }
    }

    private void BindUser()
    {
        ddlUserList.Items.Clear();

        if (!string.IsNullOrEmpty(txtHidUsers.Value))
        {
            string[] options = txtHidUsers.Value.Split('∮');

            for (int i = 0; i < options.Length; i++)
            {
                string[] item = options[i].Split('∞');
                ddlUserList.Items.Add(new ListItem(item[1], item[0]));
            }
        }
        ddlUserList.Items.Insert(0, new ListItem("--请选择--", ""));
    }

    private void InitForm()
    {
        txtHidOrg.Value = OrgHidValue;
        txtOrg.Text = OrgShowValue;
        var data = new OrgainzationManager().SearchOrgTree(MyStateManager.DomainID, MyStateManager.OrgID, null);
        if (data.Rows.Count != 1 || new Jumpton.SFA.Server.BusinessRule.System.UserManager().isManager(MyStateManager.PersonID))
        {
            OrgHelper.BindUsersDDL(MyStateManager.DomainID, this.txtHidOrg.Value, this.ddlUserList);
        }
        else
        {
            Person person = new PersonManager().Load(MyStateManager.PersonID);
            ddlUserList.Items.Clear();
            ddlUserList.Items.Add(new ListItem(person.Name, MyStateManager.UserID.ToString()));
        }
        //OrgHelper.BindUsersDDL(this.MyStateManager.DomainID, txtHidOrg.Value, ddlUserList);

        ddlUserType.DataSource = new DictionaryItemsManager().GetItemList(DictionaryEnum.Represent, MyStateManager.DomainID);
        ddlUserType.DataTextField = "Name";
        ddlUserType.DataValueField = "DictionaryItemID";
        ddlUserType.DataBind();
        ddlUserType.Items.Insert(0, new ListItem("--所有--", ""));
    }


    public void RaiseCallbackEvent(string the_string)
    {
        _content = the_string;
    }

    public string GetCallbackResult()
    {
        //  拆分由客户端传过来的参数
        string[] parts = _content.Split('|');

        //  生成ServerMethod的参数队列
        object[] theArgs = new object[parts.Length - 1];
        for (int int_index = 1; int_index < parts.Length; int_index++)
            theArgs[int_index - 1] = parts[int_index];

        //  调用ServerMethod
        return (string)GetType().GetMethod(parts[0]).Invoke(this, theArgs);
    }



    protected void ddlUserType_SelectedIndexChanged(object sender, EventArgs e)
    {
        System.Text.StringBuilder sb = new StringBuilder();

        if (string.IsNullOrEmpty(OrgHidValue))
        {
            return;
        }

        sb.AppendFormat(" MOBILE_USERS.DOMAIN_ID='{0}' ", MyStateManager.DomainID);

        sb.AppendFormat(" AND USERS.ORG_ID IN ({0}) ", OrgHidValue);

        if (!string.IsNullOrEmpty(ddlUserType.SelectedValue))
        {
            sb.AppendFormat(" AND DictionaryItems.DictionaryItemID ={0} ", ddlUserType.SelectedValue);
        }

        DataSet ds = MobileUserManager.SelectUserDDLwithType(sb.ToString());
        if (ds != null & ds.Tables.Count > 0)
        {
            ddlUserList.DataSource = ds.Tables[0];
            ddlUserList.DataTextField = "CODENAME";
            ddlUserList.DataValueField = "ID";
            ddlUserList.DataBind();
            ddlUserList.Items.Insert(0, new ListItem("--请选择--", ""));
        }
    }


    public void LoadMaps()
    {

        if (ddlUserList.SelectedValue != "")
        {
            ObjectTable ot = new VisitManager().GetVisitRoute(ddlUserList.SelectedValue, MyStateManager.DomainID, txtDate.Value);
            VISITS_ROUTE_VIEW vrv = null;
            ObjectTable ott = new VisitTypeManager().LoadTypes(MyStateManager.DomainID, null);
            lstVisits.Items.Clear();
            string visisttype;
            int i = 1;
            no = 1;
            RouteManager rm = new RouteManager();
            ListItem li = null;
            foreach (ObjectRow dr in ot)
            {
                vrv = (VISITS_ROUTE_VIEW)dr;
               
                try
                {
                    visisttype = (ott.FindByKey(vrv.VISITTYPE) as Jumpton.SFA.Server.Entity.VisitType).DESCRIPTION;
                    
                }
                catch { visisttype = vrv.VISITTYPE.ToString(); }
                string starttime = BusinessLogic.GetDateTime(vrv.STARTTIME.ToString());
                string endtime = BusinessLogic.GetDateTime(vrv.STOPTIME.ToString());
                string timespan = (DateTime.Parse(endtime) - DateTime.Parse(starttime)).Minutes + "分钟";
                li = new ListItem(i + "." + vrv.NAME, vrv.ID + "," + vrv.NAME + "," + visisttype + "," + (vrv.OUTLINE ? "线外" : "线内") + "," + (vrv.LONGITUDE == 0 ? vrv.IDENTITY1 : vrv.LONGITUDE) + "," + (vrv.LATITUDE == 0 ? vrv.IDENTITY2 : vrv.LATITUDE) + "," + BusinessLogic.GetDateTime(vrv.STARTTIME.ToString(), "yyyyMMddHHmmss", "HH:mm:ss") + "," + BusinessLogic.GetDateTime(vrv.STOPTIME.ToString(), "yyyyMMddHHmmss", "HH:mm:ss") + "," + i.ToString() + "," + timespan);
                lstVisits.Items.Add(li);
                i++;
                no++;
            }
            lstVisits.Attributes.Add("onchange", "ShowIW(this);return false;");

          
        }

    }

    protected void btnSrh_Click(object sender, EventArgs e)
    {
        
        if (ddlRoute.SelectedValue == "0")
        {
            LoadMaps();
            LoadPlanSequence();
        }
        else if (ddlRoute.SelectedValue == "1")
        {

            list_plan.Items.Clear();
            LoadMaps();
        }
        else
        {
            lstVisits.Items.Clear();
            LoadPlanSequence();
        }

    }
    protected void txtOrg_TextChanged(object sender, EventArgs e)
    {
        BindUser();
    }

    public int getWeekDay(DateTime d)
    {
        int weekday = 0;
        if (d != null)
        {
            switch (d.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    weekday = 1;
                    break;
                case DayOfWeek.Tuesday:
                    weekday = 2;
                    break;
                case DayOfWeek.Wednesday:
                    weekday = 3;
                    break;
                case DayOfWeek.Thursday:
                    weekday = 4;
                    break;
                case DayOfWeek.Friday:
                    weekday = 5;
                    break;
                case DayOfWeek.Saturday:
                    weekday = 6;
                    break;
                case DayOfWeek.Sunday:
                    weekday = 7;
                    break;
            }
        }
        return weekday;
    }

    private void LoadPlanSequence()
    {
        if (ddlUserList.SelectedValue != "")
        {
            hid_plansequence.Value = "";
            DateTime time;
            DateTime.TryParse(txtDate.Value, out time);
            int routeno = getWeekDay(time);
            ObjectTable ot = new CustomerRouteManager().GetRouteCustomerView(decimal.Parse(ddlUserList.SelectedValue), routeno);
            if (ot != null)
            {

                int rowCount = ot.Count;
                int i = 1;
               
                list_plan.Items.Clear();
                ListItem li = null;
                foreach (ROUTE_CUSTOMERS_VIEW rcv in ot)
                {
                    //li = new ListItem(i + "." + rcv.NAME, rcv.ID + "," + rcv.NAME + ",,," + rcv.IDENTITY1+ "," + rcv.IDENTITY2 + ",,," + i.ToString());
                    li = new ListItem(i + "." + rcv.NAME, rcv.NAME + "," + rcv.IDENTITY1 + "," + rcv.IDENTITY2 + "," + rcv.ADDRESSLINE + "," + rcv.TELEPHONE + "," + i.ToString()+",,,"+no.ToString());
                    list_plan.Items.Add(li);
                    //hid_plansequence.Value += rcv.NAME + "," + rcv.IDENTITY1 + "," + rcv.IDENTITY2 + "," + rcv.ADDRESSLINE + "," + rcv.TELEPHONE + "," + i.ToString() + "|";
                    i++;
                    no++;
                }
               // hid_plansequence.Value = hid_plansequence.Value.TrimEnd('|');
                list_plan.Attributes.Add("onchange", "ShowIW(this);return false;");
               
            }
        }
    }
   


}
